﻿using B.S.BaseData.Api.Read.Command.RBAC;
using B.S.BaseData.ErrorCode;
using B.S.BataBase.Domain.DTO;
using B.S.BataBase.Domain.DTO.Token;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;

namespace B.S.BaseData.Api.Read.Controllers
{
    /// <summary>
    /// 基础数据 -API  -读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        private readonly ILogger<ManagementController> _logger;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator">中介者</param>
        public ManagementController(IMediator mediator, ILogger<ManagementController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 角色列表显示
        /// </summary>
        /// <param name="command">参数</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<ApiPaging<RoleDTO>>> RoleShow([FromQuery] RoleShowCommand command)
        {
            try
            {
                _logger.LogInformation("调用了 角色列表显示 方法");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError($"调用 角色列表显示 方法异常，异常为:{ex.Message}");
                throw;
            }
        }
        /// <summary>
        /// 用户列表显示
        /// </summary>
        /// <param name="command">参数</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<ApiPaging<UserDTO>>> UserShow([FromQuery] UserShowCommand command)
        {
            try
            {
                _logger.LogInformation("调用了 用户列表显示 方法");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError($"调用 用户列表显示 方法异常，异常为:{ex.Message}");
                throw;
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="command">用户参数</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<APIResult<LoginDTO>> Login([FromQuery] LoginCommand command)
        {
            try
            {
                _logger.LogInformation("调用了 登录 方法");
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError($"调用 登录 方法异常，异常为:{ex.Message}");
                throw;
            }
        }
        /// <summary>
        /// 登录权限列表显示
        /// </summary>
        /// <param name="pathIds">路径ID列表，用逗号分隔</param>
        /// <returns>返回任务</returns>
        [HttpGet]
        public async Task<APIResult<List<PathDTO>>> PathShow([FromQuery] string pathIds)
        {
            try
            {
                _logger.LogInformation("调用了 登录权限列表显示 方法");
                PathShowCommand command = new PathShowCommand();
                command.Paths = pathIds.Split(',').Select(item => Convert.ToInt64(item)).ToList();
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                _logger.LogError($"调用 登录权限列表显示 方法异常，异常为:{ex.Message}");
                throw;
            }
        }
        /// <summary>
        /// 刷新token
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<APIResult<TokenResponse>> TokenRefresh([FromBody]RefreshTokenQueryCommand request)
        {
            try
            {
                _logger.LogInformation("调用了 刷新token 方法");
                return await _mediator.Send(request);
            }
            catch (Exception ex)
            {
                _logger.LogError($"调用 刷新token 方法异常，异常为:{ex.Message}");
                throw;
            }
        }
        /// <summary>
        /// 检测
        /// </summary>
        /// <returns>返回信息</returns>
        [HttpGet]
        public IActionResult Check()
        {
            return Ok("一切正常");
        }
    }
}
